package wholemusic.web.controller;


import com.wolfram.alpha.WAQueryResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wholemusic.web.watch.WolframAlphaAPI;
import wholemusic.web.watch.CompressUtils;
import wholemusic.web.watch.StickWatchPrivate;

import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@SuppressWarnings("unused")
@Controller
@RequestMapping("/watch")
public class StickWatchController {
    @GetMapping("/query")
    public ResponseEntity<byte[]> query(
            @RequestParam("i") String input) {
        System.out.println("---- Query: " + input);
        final long start = System.currentTimeMillis();
        WAQueryResult queryResult = WolframAlphaAPI.query(input);
        bench(start, "0");
        BufferedImage image = WolframAlphaAPI.queryImage(queryResult);
        bench(start, "1");
        HttpHeaders headers = new HttpHeaders();
        byte[] buffer = StickWatchPrivate.convertToWatchBitmap(image, headers);
        bench(start, "2");
        try {
            String filename = URLEncoder.encode(input + ".bin", "UTF-8");
            headers.set("Content-Disposition", "attachment; filename=" + filename);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        headers.set("Content-Type", "application/octet-stream");
        bench(start, "total");
        return new ResponseEntity<>(buffer, headers, HttpStatus.OK);
    }

    private void bench(long start, String tag) {
        System.out.println("------- " + tag + " cost: " + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping(value = "/compressed/{queryString}")
    public ResponseEntity<byte[]> queryCompressed(@PathVariable("queryString") String queryString) {
        WolframAlphaAPI.query(queryString);
        byte[] buffer = StickWatchPrivate.convertToWatchBitmap();
        byte[] compressed = CompressUtils.compress(buffer, buffer.length * 2);
        System.err.println("---------- COMPRESSED SIZE: " + compressed.length);
        byte[] decompressed = CompressUtils.decompress(compressed, buffer.length);
        System.err.println("---------- DECOMPRESSED SIZE: " + decompressed.length);
        for (int i = 0; i < buffer.length; i++) {
            if (buffer[i] != decompressed[i]) {
                System.err.println("---------- ERROR ---------");
                break;
            }
        }
        HttpHeaders headers = new HttpHeaders();
        try {
            String filename = URLEncoder.encode(queryString + ".bin", "UTF-8");
            headers.set("Content-Disposition", "attachment; filename=" + filename);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        headers.set("Content-Type", "application/octet-stream");
        return new ResponseEntity<>(compressed, headers, HttpStatus.OK);
    }
}
